<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use EasyWeChat;
use App\Models\User;
use Overtrue\Socialite\User as SocialiteUser;

class FrontDevMiddleware {

    /**
     * Run the request filter.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {   
        if(Auth::guest()) {
            $user = User::find(21);
            $wxuser = new SocialiteUser([
                'id' => array_get($user, 'openid'),
                'name' => array_get($user, 'name'),
                'nickname' => array_get($user, 'name'),
                'avatar' => array_get($user, 'avatar'),
                'email' => array_get($user, 'email'),
                'original' => [],
                'provider' => 'web',
            ]);

            session(['wechat.oauth_user.default' => $wxuser]);
            Auth::login($user);
        }

        return $next($request);
    }

}